package com.eqdd.yiqidian.share;

import android.content.ComponentName;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.GridLayoutManager;
import android.text.TextUtils;

import com.eqdd.yiqidian.ListItem89Custom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.GridSpacingItemDecoration;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.bean.rebuild.GridBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.rongyun.message.link.LinkContent;
import com.eqdd.yiqidian.rongyun.message.link.LinkMessage;
import com.eqdd.yiqidian.ui.share.ShareEnterActivity;
import com.eqdd.yiqidian.utils.HttpUtil;
import com.eqdd.yiqidian.utils.ShareUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.io.File;
import java.util.ArrayList;

import cn.jiguang.share.android.api.Platform;
import cn.jiguang.share.android.api.ShareParams;
import cn.jiguang.share.qqmodel.QQ;
import cn.jiguang.share.qqmodel.QZone;
import cn.jiguang.share.wechat.Wechat;
import cn.jiguang.share.wechat.WechatFavorite;
import cn.jiguang.share.wechat.WechatMoments;
import cn.jiguang.share.weibo.SinaWeibo;

import static com.eqdd.yiqidian.share.ShareDialog.ShareType.IMG;

/**
 * @author吕志豪 .
 * @date 18-2-2  上午9:42.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class ShareDialog {
    ImgShareBuilder imgShareBuilder;
    TextShareBuilder textShareBuilder;
    LinkShareBuilder linkShareBuilder;
    private ShareType type;
    private BaseActivity baseActivity;
    private Intent intent;
    private ShareParams shareParams;
    private BottomSheetDialog shareDialog;

    public ShareDialog(ImgShareBuilder imgShareBuilder, BaseActivity baseActivity) {
        this.baseActivity = baseActivity;
        this.imgShareBuilder = imgShareBuilder;
        type = IMG;
        initDialog();
    }

    public ShareDialog(TextShareBuilder textShareBuilder, BaseActivity baseActivity) {
        this.baseActivity = baseActivity;
        this.textShareBuilder = textShareBuilder;
        type = ShareType.TEXT;
        initDialog();
    }

    public ShareDialog(LinkShareBuilder linkShareBuilder, BaseActivity baseActivity) {
        this.baseActivity = baseActivity;
        this.linkShareBuilder = linkShareBuilder;
        type = ShareType.LINK;
        initDialog();
    }

    private void initDialog() {
        shareDialog = new BottomSheetDialog(baseActivity);
        ListItem89Custom inflate = DataBindingUtil.inflate(baseActivity.getLayoutInflater(), R.layout.list_item_89, null, false);
        inflate.recyclerView.setLayoutManager(new GridLayoutManager(baseActivity, 4));
        inflate.recyclerView.setNestedScrollingEnabled(false);
        inflate.recyclerView.addItemDecoration(
                new GridSpacingItemDecoration(4, 50, true));
        ArrayList<GridBean> gridBeans = new ArrayList<>();
        gridBeans.add(new GridBean("发送给好友", R.mipmap.share_friend, 0));
        gridBeans.add(new GridBean("工作圈", R.mipmap.share_eqd, 1));
        gridBeans.add(new GridBean("我的收藏", R.mipmap.share_shoucang, 2));
        if (type == ShareType.LINK) {
            gridBeans.add(new GridBean("用浏览器打开", R.mipmap.share_liulanqi));
        }
        gridBeans.add(new GridBean("微信", R.mipmap.wechat, 4));
        gridBeans.add(new GridBean("微信收藏", R.mipmap.wechat_fav, 5));
        gridBeans.add(new GridBean("朋友圈", R.mipmap.wechat_moment, 6));
        gridBeans.add(new GridBean("QQ", R.mipmap.qq, 7));
        gridBeans.add(new GridBean("qq空间", R.mipmap.qzone, 8));
        gridBeans.add(new GridBean("新浪微博", R.mipmap.weibo, 9));
        SlimAdapterEx<GridBean> slimAdapterEx = SlimAdapterEx.create().register(R.layout.grid_item, new SlimInjector<GridBean>() {
            @Override
            public void onInject(GridBean data, IViewInjector injector) {
                injector.text(R.id.ItemText, data.getTitle())
                        .image(R.id.ItemImage, data.getResId())
                        .gone(R.id.red_dot);
            }
        }).attachTo(inflate.recyclerView).updateData(gridBeans);
        ItemClickSupport.addTo(inflate.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    switch (slimAdapterEx.getDataItem(position).getPos()) {
                        case 0:
                            share2EQD();
                            break;
                        case 1:
                            break;
                        case 2:
                            share2Collection();
                            break;
                        case 3:
                            Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(linkShareBuilder.getUrl()));
                            baseActivity.startActivity(in);
                            break;
                        case 4:
                            share2WeXin(Wechat.Name);
                            break;
                        case 5:
                            share2WeXin(WechatFavorite.Name);
                            break;
                        case 6:
                            share2WeXin(WechatMoments.Name);
                            break;
                        case 7:
                            share2Q(QQ.Name);
                            break;
                        case 8:
                            share2Q(QZone.Name);
                            break;
                        case 9:
                            share2SinaWeibo();
                            break;
                    }
                    shareDialog.dismiss();
                });
        shareDialog.setContentView(inflate.getRoot());
        shareDialog.setCancelable(true);
        shareDialog.setCanceledOnTouchOutside(true);
    }

    public void show() {
        shareDialog.show();
    }

    private void share2EQD() {
        switch (type) {
            case IMG:
                intent = new Intent(Intent.ACTION_SEND);
                intent.setType("image/*");
                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imgShareBuilder.getImagePath())));
                intent.setComponent(new ComponentName("com.eqdd.yiqidian", "com.eqdd.yiqidian.ui.share.ShareEnterActivity"));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                baseActivity.startActivity(intent);
                break;
            case TEXT:
                intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain"); // 纯文本
                intent.putExtra(Intent.EXTRA_SUBJECT, textShareBuilder.getTitle());
                intent.putExtra(Intent.EXTRA_TEXT, textShareBuilder.getText());
                intent.setComponent(new ComponentName("com.eqdd.yiqidian", "com.eqdd.yiqidian.ui.share.ShareEnterActivity"));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                baseActivity.startActivity(intent);
                break;
            case LINK:
                LinkContent linkContent = new LinkContent(linkShareBuilder.getTitle(), linkShareBuilder.getText(), linkShareBuilder.getUrl(),
                        linkShareBuilder.getImageUrl(), linkShareBuilder.getSource());
                AppConstent.messageContent = LinkMessage.obtain(linkContent);
                baseActivity.startActivity(ShareEnterActivity.class);
                break;
        }
    }

    private void share2WeXin(String platform) {
        switch (type) {
            case IMG:
                shareParams = new ShareParams();
                shareParams.setShareType(Platform.SHARE_IMAGE);
                shareParams.setImagePath(imgShareBuilder.getImagePath());
                break;
            case TEXT:
                shareParams = new ShareParams();
                shareParams.setShareType(Platform.SHARE_TEXT);
                shareParams.setText(textShareBuilder.getText());//必须
                break;
            case LINK:
                shareParams = new ShareParams();
                shareParams.setTitle(linkShareBuilder.getTitle());
                shareParams.setText(linkShareBuilder.getText());
                shareParams.setShareType(Platform.SHARE_WEBPAGE);
                shareParams.setUrl(linkShareBuilder.getUrl());//必须
                shareParams.setImagePath(linkShareBuilder.getImagePath());
                break;

        }
        ShareUtil.share(platform, shareParams);

    }

    private void share2Q(String platform) {
        switch (type) {
            case IMG:
                shareParams = new ShareParams();
                shareParams.setShareType(Platform.SHARE_IMAGE);
                if (TextUtils.isEmpty(imgShareBuilder.getImageUrl())) {
                    shareParams.setImagePath(imgShareBuilder.getImagePath());
                } else {
                    shareParams.setImageUrl(imgShareBuilder.getImageUrl());
                }
                break;
            case TEXT:
                shareParams = new ShareParams();
                shareParams.setShareType(Platform.SHARE_TEXT);
                shareParams.setText(textShareBuilder.getText());
            case LINK:
                shareParams = new ShareParams();
                shareParams.setTitle(linkShareBuilder.getTitle());
                shareParams.setText(linkShareBuilder.getText());
                shareParams.setShareType(Platform.SHARE_WEBPAGE);
                shareParams.setUrl(linkShareBuilder.getUrl());//必须
                if (TextUtils.isEmpty(imgShareBuilder.getImageUrl())) {
                    shareParams.setImagePath(imgShareBuilder.getImagePath());
                } else {
                    shareParams.setImageUrl(imgShareBuilder.getImageUrl());
                }
                break;
        }
        ShareUtil.share(platform, shareParams);
    }

    private void share2SinaWeibo() {
        switch (type) {
            case IMG:
                shareParams = new ShareParams();
                shareParams.setShareType(Platform.SHARE_IMAGE);
                shareParams.setImagePath(imgShareBuilder.getImagePath());
                break;
            case LINK:
                shareParams = new ShareParams();
                shareParams.setShareType(Platform.SHARE_WEBPAGE);
                shareParams.setText(linkShareBuilder.getText());
                shareParams.setImagePath(linkShareBuilder.getImagePath());
                shareParams.setUrl(linkShareBuilder.getUrl());
        }
        ShareUtil.share(SinaWeibo.Name, shareParams);
    }

    private void share2Collection() {
        switch (type) {
            case IMG:
                HttpUtil.addImgToCollection(imgShareBuilder.getSource(), imgShareBuilder.getSourceOwner(),
                        imgShareBuilder.getImageUrl(), imgShareBuilder.getImagePath());
                break;
            case TEXT:
                HttpUtil.addTextToCollection(textShareBuilder.getSource(), textShareBuilder.getSourceOwner(),
                        textShareBuilder.getText(), textShareBuilder.getTitle());
                break;
            case LINK:
                HttpUtil.addLinkToCollection(imgShareBuilder.getSource(), imgShareBuilder.getSourceOwner(),
                        imgShareBuilder.getImageUrl(), imgShareBuilder.getImagePath());
                break;

        }
    }
    enum ShareType {
        TEXT,
        IMG,
        LINK,
        FILE,
        VIDEO
    }
}
